package android.Spider;

import java.util.ArrayList;

import android.content.Intent;
import android.view.View;
import android.widget.ListView;
import android.widget.Toast;

public class Broodmother extends PictureConnectionListActivity
{

	@Override
	void InitConnection() 
	{
        PrivateMessagesList.s_brMother = this;
        PollReceive.m_brMother = this;
        UserProfile.m_brMother = this;
		
        m_connection = new XmppConnection() {
        	
    		@Override
			protected void OnConnected() {
    			// Initialize fetching of all profiles from server
    			RequestAllProfiles();
			}

			@Override
			protected void OnProfilesReceived(ArrayList<ProfileUser> lstProfiles) {
				SpiderPreferences.Instance(Broodmother.this).SetAllProfiles(lstProfiles);
			}

			@Override
    		public void OnQuestionReceived(int idRequest, long idQuestion) {
    			Toast.makeText(Broodmother.this, "You have received a new question", Toast.LENGTH_SHORT).show();
    			UpdateReceivedInquires();
    		}

    		@Override
    		protected void OnAnswerSent(long idQuestion) {
    			UpdateReceivedInquires();
    		}

    		@Override
    		protected void OnMessageReceived(int idMessage) {
    			Toast.makeText(Broodmother.this, "You have received a new private message", Toast.LENGTH_SHORT).show();
    			UpdateReceivedMessage();
    		}
    		
        };

        super.InitConnection();
	}

	@Override
	void InitListAdapter() {
		super.InitListAdapter();
		
		String arrDesc[] = { 
				GetProfileComplete(), 
				"Discussions on message boards",
				GetUnreadCount(),
				"Create or manage my inquiries",
				GetUnansweredCount(),
				"Search for friends"
				};
		
		final int nProfileComplete = SpiderPreferences.Instance(this).GetProfileCompleteness();
		int nProfileResource = (nProfileComplete == 0) ? R.drawable.profile0 :
			(nProfileComplete <= 33) ? R.drawable.profile33 :
				(nProfileComplete <= 66) ? R.drawable.profile66 : R.drawable.profile100;

		int nUnreadCount = PrivateMessages.Instance(this).GetUnreadCount(); 
		int nUnanswered = Questions.Instance(this).GetUnansweredCount();
		
		int arrPicture[] = {
				nProfileResource,
				R.drawable.messageboard,
				(nUnreadCount == 0) ? R.drawable.privatemessagesread : R.drawable.privatemessagesunread,
				R.drawable.inquiries,
				(nUnanswered == 0) ? R.drawable.recivedinqueriesnomsg : R.drawable.recivedinqueriesmsg,
				R.drawable.friendsearch
		};

		ArrayList<ListItem> lstItems = new ArrayList<ListItem>(5);
		for (int i = 0; i < 6; ++i)
		{
			ListItem item = new ListItem();
			item.m_strDesc = arrTitle[i];
			item.m_strFineDesc = arrDesc[i];
			item.m_bmpPicture = getResources().getDrawable(arrPicture[i]);
			lstItems.add(item);
		}
		
		SetListItems(lstItems);
	}

	private String GetProfileComplete()
	{
		return "Profile is " + SpiderPreferences.Instance(this).GetProfileCompleteness() + "% complete";
	}
	
	private String GetUnreadCount()
	{
		int nUnreadCount = PrivateMessages.Instance(this).GetUnreadCount(); 
		String strPrivateMessages;
		if (nUnreadCount == 0)
			strPrivateMessages = "No unread messages";
		else if (nUnreadCount == 1)
			strPrivateMessages = "You have 1 unread message";
		else
			strPrivateMessages = "You have " + nUnreadCount + " unread messages";
		
		return strPrivateMessages;
	}
	
	private String GetUnansweredCount()
	{
		int nUnanswered = Questions.Instance(this).GetUnansweredCount();
		String strUnanswered;
		if (nUnanswered == 0)
			strUnanswered = "No new questions";
		else if (nUnanswered == 1)
			strUnanswered = "You have 1 new question";
		else
			strUnanswered = "You have " + nUnanswered + " new questions";
		
		return strUnanswered;
	}
	
	@Override
	protected void onDestroy() {
        PrivateMessagesList.s_brMother = null;
        PollReceive.m_brMother = null;
        UserProfile.m_brMother = null;

		super.onDestroy();
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id)
	{
		if (arrClass[position] != null)
		{
			if (position == 4 && Questions.Instance(this).GetUnansweredCount() == 0)
			{
				// There are no new inquiries
				Toast.makeText(this, "There are no new inquiries.", Toast.LENGTH_LONG).show();
			}
			else
			{
				Intent intent = new Intent(this, arrClass[position]);
				startSubActivity(intent, 0);
			}
		}
		else
		{
			Toast.makeText(this, "Not implemented yet. Sorry for inconvinience.", Toast.LENGTH_LONG).show();
		}
	}

	void UpdateProfileInfo()
	{
		ListItem item = new ListItem();
		item.m_strDesc = arrTitle[0];
		item.m_strFineDesc = GetProfileComplete();
		final int nProfileComplete = SpiderPreferences.Instance(this).GetProfileCompleteness();
		int nResource = (nProfileComplete == 0) ? R.drawable.profile0 :
			(nProfileComplete <= 33) ? R.drawable.profile33 :
				(nProfileComplete <= 66) ? R.drawable.profile66 : R.drawable.profile100;
		
		item.m_bmpPicture = getResources().getDrawable(nResource);
		ResetItem(0, item);

		SpiderListener.UpdateNotification(this);
	}

	void UpdateReceivedInquires()
	{
		ListItem item = new ListItem();
		item.m_strDesc = arrTitle[4];
		item.m_strFineDesc = GetUnansweredCount();
		item.m_bmpPicture = getResources().getDrawable((Questions.Instance(this).GetUnansweredCount() == 0) ? R.drawable.recivedinqueriesnomsg : R.drawable.recivedinqueriesmsg);
		ResetItem(4, item);
		
		SpiderListener.UpdateNotification(this);
	}

	void UpdateReceivedMessage()
	{
		ListItem item = new ListItem();
		item.m_strDesc = arrTitle[2];
		item.m_strFineDesc = GetUnreadCount();
		item.m_bmpPicture = getResources().getDrawable((PrivateMessages.Instance(this).GetUnreadCount() == 0) ? R.drawable.privatemessagesread : R.drawable.privatemessagesunread);
		ResetItem(2, item);

		SpiderListener.UpdateNotification(this);
	}

	private final String arrTitle[] = { 
			"Profile data", 
			"Board messages", 
			"Private messages", 
			"Inquiries", 
			"Received inquiries",
			"Friend search"
	};

	@SuppressWarnings("unchecked")
	private final Class arrClass[] = {
			UserProfile.class,
			MessageBoard.class,
			PrivateMessagesList.class,
			ManageInquiries.class,
			PollReceive.class,
			FriendSearch.class
	};

}
